﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using System.Web.Security;

public partial class Page_SystemFrameworkEdit : System.Web.UI.Page
{
    SystemFrameworkData SuperiorSFD;
    SystemFrameworkData sfd1;
    protected void Page_Load(object sender, EventArgs e)
    {
        string strid = Request.QueryString["id"];
        string strsid = Request.QueryString["sid"];
        Guid sid = Guid.Parse(strsid);
        sfd1 = null;
        if (sid == Guid.Empty)
        {
            TextBox1.Text = "系统平台";
            SuperiorSFD = null;
        }
        else
        {
            SuperiorSFD = SystemFrameworkDataDBOption.SelectByID(sid);
            TextBox1.Text = SuperiorSFD.Name;
        }
        if (strid == null || strid == "")
        {

        }
        else
        {
            Guid id = Guid.Parse(strid);
            sfd1 = SystemFrameworkDataDBOption.SelectByID(id);            
        }
        if(!IsPostBack)
        {
            if (sfd1 != null)
            {
                TextBox2.Enabled = false;
                TextBox2.Text = sfd1.Name;
                TextBox3.Text = sfd1.Contact;
                TextBox4.Text = sfd1.Organization;
                TextBox5.Text = sfd1.Telephone;
                TextBox6.Text = sfd1.Address;
                TextBox7.Text = sfd1.MeasurementCountLimit.ToString();
                TextBox8.Text = sfd1.Remark;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SystemFrameworkData sfd = new SystemFrameworkData();
        sfd.Name = TextBox2.Text;
        sfd.Contact = TextBox3.Text;
        sfd.Organization = TextBox4.Text;
        sfd.Telephone = TextBox5.Text;
        sfd.Address = TextBox6.Text;
        sfd.MeasurementCountLimit = int.Parse(TextBox7.Text);
        sfd.Remark = TextBox8.Text;
        if (SuperiorSFD == null)
        {
            sfd.FrameLevel = 1;
            sfd.SuperiorID = Guid.Empty;
        }
        else
        {
            sfd.FrameLevel = SuperiorSFD.FrameLevel + 1;
            sfd.SuperiorID = SuperiorSFD.ID;
        }
        if (sfd1 == null)
        {
            sfd.CreateTime = DateTime.Now;
        }
        else
        {
            sfd.CreateTime = sfd1.CreateTime;
        }
        sfd.UpdateTime = DateTime.Now;
        sfd.Creator = Membership.GetUser().UserName;
        if(sfd1==null)
        {
            SystemFrameworkDataDBOption.Insert(sfd);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('添加成功');GetDataAndClose(); </script>");
        }
        else
        {
            sfd.ID = sfd1.ID;
            SystemFrameworkDataDBOption.Update(sfd);
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", " <script lanuage=javascript>alert('修改成功');GetDataAndClose(); </script>");
        }
    }
}